import java.util.LinkedList;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode createBinaryTree(){
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');
        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        E.right=H;
        C.left=F;
        C.right=G;
        return A;
    }
    //前序遍历
    public void preOrder(TreeNode root){
        if(root==null) return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历
    public void inOrder(TreeNode root){
        if(root==null) return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    //后序遍历
    public void postOrder(TreeNode root){
        if(root==null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    //层序遍历
    public void levelOrder(TreeNode root){
        if(root==null) return;
        Queue<TreeNode> cur=new LinkedList<>();
        cur.offer(root);
        while(!cur.isEmpty()){
            TreeNode res=cur.poll();
            System.out.print(res.val+" ");
            if(res.left!=null){
                cur.offer(res.left);
            }
            if(res.right!=null){
                cur.offer(res.right);
            }
        }
    }
    //获取树中节点的个数
    //方法1
//    int usedsize=0;
//    public void size(TreeNode root){
//        if(root==null){
//            return;
//        }
//        usedsize++;
//        size(root.left);
//        size(root.right);
//    }
    //方法2
    public int size(TreeNode root){
        if(root==null) return 0;
        return size(root.left)+size(root.right)+1;
    }
    //获取叶子结点的个数
    //方法1
//    public int leafCount=0;
//    public void getLeafNodeCount(TreeNode root){
//        if(root==null){
//            return;
//        }
//        if(root.left==null&&root.right==null){
//            leafCount++;
//        }
//        getLeafNodeCount(root.left);
//        getLeafNodeCount(root.right);
//    }
    //方法2
    public int getLeafNodeCount(TreeNode root){
        if(root==null){
            return 0;
        }
        if(root.right==null&&root.left==null){
            return 1;
        }
        return getLeafNodeCount(root.left)+getLeafNodeCount(root.right);
    }
    //检测元素key是否存在
    public TreeNode find(TreeNode root,char key){
        if(root==null) return null;
        if(root.val==key){
            return root;
        }
        TreeNode leftResult=find(root.left,key);
        if(leftResult!=null){
            return leftResult;
        }
        TreeNode rightResult=find(root.right,key);
        if(rightResult!=null){
            return rightResult;
        }
        return null;
    }
    //翻转二叉树
    public TreeNode invertTree(TreeNode root){
        if(root==null) return null;
        TreeNode tem=root.left;
        root.left=root.right;
        root.right=tem;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    //获取树的高度
    public int getHeight(TreeNode root){
        if(root==null) return 0;
        int leftHeight=getHeight(root.left);
        int rightHeight=getHeight(root.right);
        return Math.max(leftHeight,rightHeight)+1;
    }
}
